package com.zsy.service.impl;

import com.zsy.entity.InvoiceManagement;
import com.zsy.entity.User;
import com.zsy.mapper.IFinanceMapper;
import com.zsy.service.IFinanceService;
import com.zsy.utils.Result;
//import com.zsy.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FinanceServiceImpl implements IFinanceService {
    @Autowired
    private IFinanceMapper ifm;
    // 查询发票信息
    @Override
    public Result selectInvoice() {
//        Map<String, Object> mp = ThreadLocalUtil.get();
//        String department = (String) mp.get("department");
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        User principal = (User) authentication.getPrincipal();
//        if(department.equals("财务部") || department.equals("管理员")){
            List<InvoiceManagement> invoiceManagements = ifm.selectInvoice();
            for (InvoiceManagement invoiceManagement : invoiceManagements) {
                System.out.println(invoiceManagement.toString());
            }
            if(invoiceManagements != null){
                return Result.ok(invoiceManagements);
            } else {
                return Result.error("未获取到数据");
            }
//        }
//        else {
//            return Result.error("没有权限");
//        }

    }

    // 删除发票信息
    @Override
    public Result deleteInvoice(int id) {
        int i = -1;
        i = ifm.deleteInvoice(id);
        if (i > 0) {
            return Result.ok("发票删除成功");
        } else {
            return Result.error("发票删除失败");
        }
    }

    // 新增发票信息
    @Override
    public Result insertInvoice(InvoiceManagement im) {
        int i = -1;
        i = ifm.insertInvoice(im);
        if (i > 0) {
            return Result.ok("发票添加成功");
        } else {
            return Result.error("发票添加失败");
        }
    }
}
